﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.ServiceLocation;
using Spring.Web.Mvc;
using eDirectory.Common.DependencyInjection;
using eDirectory.Common.Dto.Address;
using eDirectory.Common.Dto.Customer;
using eDirectory.Domain.AppServices;
using eDirectory.Domain.Entities;
using eDirectory.Domain.Services;
using eDirectory.Mvc.DependencyInjection;

namespace eDirectory.Mvc
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : SpringMvcApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Customer", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            AutoMapperConfigurator.Install();
            //InitialiseFixtures();
        }

        protected override void Application_BeginRequest(object sender, EventArgs e)
        {
            base.Application_BeginRequest(sender,e);
            Container.RequestContext = ServiceLocator.Current.GetInstance<IRequestContext>();
        } 

        private void InitialiseFixtures()
        {
            var service = new CustomerService();
            var dto = new CustomerDto {FirstName = "Enrique", LastName = "Albert", Telephone = "999-0000"};
            
            var customer = service.CreateNewCustomer(dto);
            var addressDto = new AddressDto();
            addressDto.City = "Cottesloe";
            addressDto.Country = "Australia";
            addressDto.PostCode = "6011";
            addressDto.Street = "Salvado St";
            addressDto.CustomerId = customer.Id;

            var addressService = new AddressService();
            addressService.CreateNewAddress(addressDto);

        }

        public override void RegisterDependencyResolver(IDependencyResolver resolver)
        {
            base.RegisterDependencyResolver(resolver);
            var springResolver = resolver as SpringMvcDependencyResolver;
            if (springResolver == null) return;
            ServiceLocator.SetLocatorProvider(() => new SpringServiceLocatorAdapter(springResolver));
            //DiContext.AppContext = springResolver.ApplicationContext;
        }
    }
}